package com.note.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Package：com.note.common.utils
 * @Name：JedisUtils
 * @Author：热伊木
 * @Email：uyevan@163.com
 * @Date：2024-04-29-22:25
 * @Description：Jedis连接池工具类
 */

@Component
public class JedisUtils {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 存储字符串
     * @param key
     * @param value
     */
    public void set (String key, Object value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, String.valueOf(value));
        }catch (Exception ignore){}finally {
            jedisPool.returnResource(jedis);
        }
    }

    /**
     * 获取value
     * @param key
     * @return
     */
    public String get(String key){
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            return "-1";
        }
    }

    /**
     * 移除Value
     * @param key
     * @return
     */
    public Long del(String key){
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.del(key);
        } catch (Exception e) {
            return -1L;
        }
    }


    /**
     * 判断是否存在
     * @param key
     * @return
     */
    public Boolean exists(String key){
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.exists(key);
        } catch (Exception e) {
            return false;
        }
    }

}
